﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;
using FineUI;
using System.Data;
using ZHDJ.Data;

namespace JoinParty.admin.FlowStep
{
    public partial class MeetingStepEdit : PageBase
    {
        public string ID
        {
            get
            {
                return GetQueryIntValue("id");
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        /// <summary>
        /// 申请流程环节是否已处理
        /// </summary>
        public bool IsHandle
        {
            get;
            set;
        }
        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
            if (IsHandle)
            {
                Alert.Show("该申请单流程已处理!");
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        private void BindData()
        {
            ZHDJ.WebPage.ControlBusiness.ControlBind(this.ddlBackStep, PartyJoinProcess.GetGirdData("", PartyJoinProcess.GetCurrentStepNo(this.CurrentStepID)), "ProcessName", "ID");
        }

        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (oModel == null)
            {
                IsHandle = false;
            }
            else if (string.IsNullOrEmpty(oModel.AppllicationID))
            {
                IsHandle = false;
            }

            if (!string.IsNullOrEmpty(oModel.Remark))
            {
                this.heMeetingStepEdit.Text = oModel.Remark;
            }
            this.txtAgree.Text = oModel.Agree.ToString();
            this.txtWaiver.Text = oModel.Waiver.ToString();
            this.txtDisAgree.Text = oModel.NotAgree.ToString();
            this.ddlStatue.SelectedValue = oModel.Status.ToString();
            this.tgbMeetingTime.Text = Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss");

            if (!string.IsNullOrEmpty(oModel.MeetingID))
            {
                var JMoModel = DB.partyJoinMeetings.Find(oModel.MeetingID);
                if (JMoModel != null)
                {
                    this.txtShouldBeTo.Text = JMoModel.ShouldBeTo.ToString();
                    this.txtIsTo.Text = JMoModel.IsTo.ToString();
                    this.tgbMeetingTime.Text = Convert.ToDateTime(JMoModel.MeetingTime ?? DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
            else if (this.ddlStatue.SelectedValue == "2" && this.CurrentStepID == ProcesUtil.JoinStep24)
            {
                var mb = DB.PartyJoinMeetingBacksteps.Find(this.CurrentStandID);
                if (mb != null)
                {
                    this.ddlBackStep.SelectedValue = mb.BackStepID;
                    var jm = DB.partyJoinMeetings.Find(mb.MeetingID);
                    if (jm != null)
                    {
                        this.txtShouldBeTo.Text = Convert.ToString(jm.ShouldBeTo);
                        this.txtIsTo.Text = Convert.ToString(jm.IsTo);
                    }
                }
            }
            else
            {
                this.txtShouldBeTo.Text = "0";
                this.txtIsTo.Text = "0";
            }

            // 页面只读显示
            if (this.ReadOnly.Trim() == "1")
            {
                this.btnReCell.Visible = false;
                this.btnSubmit.Visible = false;
                if (IsJoinPartyAdmin)
                {
                    this.btnSaveClose.Visible = true;
                    this.txtAgree.Enabled = false;
                    this.txtWaiver.Enabled = false;
                    this.txtDisAgree.Enabled = false;
                    this.ddlStatue.Enabled = false;
                    this.txtShouldBeTo.Enabled = false;
                    this.txtIsTo.Enabled = false;
                }
            }
        }



        private void SaveData(bool isClose, bool AdminSave = false)
        {
            string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
            string outMassage = "";

            bool bResult = false;

            if (AdminSave)
            {
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                oModel.Remark = this.heMeetingStepEdit.Text;
                bResult = DB.SaveChanges() > 0;
            }
            else
            {
                #region
                var oModel = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
                oModel.Agree = Convert.ToInt32(this.txtAgree.Text);
                oModel.Waiver = Convert.ToInt32(this.txtWaiver.Text);
                oModel.NotAgree = Convert.ToInt32(this.txtDisAgree.Text);
                oModel.Remark = this.heMeetingStepEdit.Text;
                if (Convert.ToInt32(ddlStatue.SelectedValue) == (int)Enum_FlowStanceStatus.未处理 ||
                    Convert.ToInt32(ddlStatue.SelectedValue) == (int)Enum_FlowStanceStatus.通过)
                {
                    bResult = DB.SaveChanges() > 0;
                }
                else
                {
                    oModel.Status = Convert.ToInt32(this.ddlStatue.SelectedValue);
                    oModel.Remark = this.heMeetingStepEdit.Text;
                    oModel.HandleUserID = this.CurrentUser.LoginID;
                    oModel.HandleUserName = this.CurrentUser.UserName;
                    oModel.HandleTime = DateTime.Now;
                    oModel.Remark = this.heMeetingStepEdit.Text;
                    if (this.ddlStatue.SelectedValue == "2" && this.CurrentStepID == ProcesUtil.JoinStep24)
                    {
                        bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message, BackStep: this.ddlBackStep.SelectedValue);
                    }
                    else
                    {
                        bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);

                    }
                }
                #endregion

                #region 对会议表进行操作，会议不存在则新建会议并关联会议相关的流程实例,会议已存在则更新
                if (string.IsNullOrEmpty(oModel.MeetingID))
                {
                    partyJoinMeeting JMoModel = new partyJoinMeeting();
                    JMoModel.MeetingID = StringHelper.NewID;
                    JMoModel.Title = "";
                    JMoModel.Content = "";
                    JMoModel.Remark = "";
                    JMoModel.MeetingTime = Convert.ToDateTime(this.tgbMeetingTime.Text);
                    JMoModel.ShouldBeTo = Convert.ToInt32(this.txtShouldBeTo.Text.Trim());
                    JMoModel.IsTo = Convert.ToInt32(this.txtIsTo.Text.Trim());
                    DB.partyJoinMeetings.Add(JMoModel);
                    DB.SaveChanges();

                    //退回时候关联会议表
                    if (this.ddlStatue.SelectedValue == "2" && this.CurrentStepID == ProcesUtil.JoinStep24)
                    {
                        //退回信息入表
                        var mb = DB.PartyJoinMeetingBacksteps.Find(this.CurrentStandID);
                        mb.BackStepID = this.ddlBackStep.SelectedValue;
                        mb.MeetingID = JMoModel.MeetingID;
                        DB.SaveChanges();
                    }

                    #region 会议和流程实例建立关系
                    DataSet ds = ModuleUtil.BranchLoginStantID(this.ApplyBranchID, this.CurrentStepID,this.CurrentStandID);
                    PartyJoinLinkExample sModel = null;
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        sModel = DB.PartyJoinLinkExamples.Find(dr["StandID"].ToString());
                        //保存时更新流程实例的会议ID
                        sModel.MeetingID = JMoModel.MeetingID;
                        DB.SaveChanges();
                    }
                    #endregion

                }
                else
                {

                    var JMoModel = DB.partyJoinMeetings.Find(oModel.MeetingID);
                    JMoModel.ShouldBeTo = Convert.ToInt32(this.txtShouldBeTo.Text.Trim());
                    JMoModel.IsTo = Convert.ToInt32(this.txtIsTo.Text.Trim());
                    JMoModel.MeetingTime = Convert.ToDateTime(this.tgbMeetingTime.Text);
                    if (!Utility.CheckFieldUnique("tblpartyjoinmeeting", "MeetingID", oModel.MeetingID, ""))
                    {
                        DB.SaveChanges();
                    }
                    else
                    {
                        DB.partyJoinMeetings.Add(JMoModel);
                        DB.SaveChanges();
                    }
                }
                #endregion
            }

            if (bResult)
            {
                if (AdminSave)
                    Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
                else
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
            }
            else
            {
                if (Message != "")
                {
                    Alert.Show(Message, String.Empty, ActiveWindow.GetHideReference());
                }
            }
        }

        #endregion

        #region 按钮
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int Agree = Convert.ToInt32(this.txtAgree.Text.Trim());
            int NotAgree = Convert.ToInt32(this.txtDisAgree.Text.Trim());
            int Waiver = Convert.ToInt32(this.txtWaiver.Text.Trim());
            int ShouldBeTo = Convert.ToInt32(this.txtShouldBeTo.Text.Trim());
            int IsTo = Convert.ToInt32(this.txtIsTo.Text.Trim());
            string errMsg = string.Empty;
            //规则判断
            if (!PartyJoinLinkExample.CheckRule(Agree, Waiver, NotAgree, ShouldBeTo, IsTo, out errMsg, "", this.ddlStatue.SelectedValue))
            {
                Alert.Show(errMsg, String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            else
            {
                this.SaveData(true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        /// <summary>
        /// 保存并关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        /// <summary>
        /// 撤回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReCell_Click(object sender, EventArgs e)
        {
            string tipMessage = "";
            PartyJoinLinkExample.ReLastStep(this.CurrentStandID, this.AppllicationID, out tipMessage, this.CurrentUser.LoginID);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void ddlStatue_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlStatue.SelectedValue == "2" && this.CurrentStepID == ProcesUtil.JoinStep24)
            {
                this.ddlBackStep.Hidden = false;
                this.ddlBackStep.SelectedValue = ProcesUtil.JoinStep19;
            }
            else
            {
                this.ddlBackStep.Hidden = true;
            }
        }
        #endregion
    }
}